<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="SKU编码" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入SKU编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属店铺" prop="shopId">
        <el-select v-model="queryParams.shopId" placeholder="请选择店铺" clearable>
          <el-option
            v-for="item in shopOptions"
            :key="item.shopId"
            :label="item.shopName"
            :value="item.shopId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="analysisList">
      <el-table-column label="SKU" align="center" prop="sku" width="140" :show-overflow-tooltip="true" fixed="left" />
      <el-table-column label="SKU货号" align="center" prop="skuCode" width="120" :show-overflow-tooltip="true" />
      <el-table-column label="产品名称" align="center" prop="productName" width="140" :show-overflow-tooltip="true" fixed="left" />
      <el-table-column label="SKU属性" align="center" prop="skuAttr" width="110" :show-overflow-tooltip="true" />

      <el-table-column label="产品单个平均成本" align="center" prop="productAvgCost" width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.productAvgCost != null ? '¥' + scope.row.productAvgCost.toFixed(2) : '¥0.00' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="核价" align="center" prop="priceCheck" width="110">
        <template slot-scope="scope">
          <span>{{ scope.row.priceCheck != null ? '¥' + scope.row.priceCheck.toFixed(2) : '¥0.00' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="单个平均收入金额" align="center" prop="avgIncomeAmount" width="150">
        <template slot-scope="scope">
          <span style="color: #67C23A; font-weight: bold;">{{ scope.row.avgIncomeAmount != null ? '¥' + scope.row.avgIncomeAmount.toFixed(2) : '¥0.00' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="利润" align="center" prop="profit" width="120">
        <template slot-scope="scope">
          <span :style="getProfitStyle(scope.row.profit)">{{ scope.row.profit != null ? '¥' + scope.row.profit.toFixed(2) : '¥0.00' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="利润率(%)" align="center" prop="profitRate" width="110">
        <template slot-scope="scope">
          <el-tag :type="getProfitRateType(scope.row.profitRate)" effect="dark">
            {{ scope.row.profitRate != null ? scope.row.profitRate.toFixed(2) + '%' : '0%' }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="售后率(%)" align="center" prop="aftersaleRate" width="110">
        <template slot-scope="scope">
          <span style="color: #F56C6C;">{{ scope.row.aftersaleRate != null ? scope.row.aftersaleRate.toFixed(2) + '%' : '0%' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="所属店铺" align="center" prop="shopName" width="110" />
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listProductAnalysis } from "@/api/business/analysis"
import { getShopList } from "@/api/system/user"

export default {
  name: "ProductAnalysis",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品分析表格数据
      analysisList: [],
      // 店铺选项
      shopOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        productName: null,
        shopId: null
      }
    }
  },
  created() {
    this.getList()
    this.getShopOptions()
  },
  methods: {
    /** 查询产品分析列表 */
    getList() {
      this.loading = true
      listProductAnalysis(this.queryParams).then(response => {
        this.analysisList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 获取店铺选项 */
    getShopOptions() {
      getShopList().then(response => {
        this.shopOptions = response.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/analysis/product/export', {
        ...this.queryParams
      }, `产品分析_${new Date().getTime()}.xlsx`)
    },
    /**
     * 获取售后率标签类型
     * <5% 绿色，5%-10% 橙色，>10% 红色
     */
    getAftersaleRateType(aftersaleRate) {
      if (aftersaleRate == null) {
        return 'success'
      }
      if (aftersaleRate > 10) {
        return 'danger'  // 红色
      } else if (aftersaleRate > 5) {
        return 'warning'  // 橙色
      } else {
        return 'success'  // 绿色
      }
    },
    /**
     * 获取利润率标签类型
     * <20% 红色，20%-50% 橙色，>50% 绿色
     */
    getProfitRateType(profitRate) {
      if (profitRate == null) {
        return ''
      }
      if (profitRate < 20) {
        return 'danger'  // 红色
      } else if (profitRate <= 50) {
        return 'warning'  // 橙色
      } else {
        return 'success'  // 绿色
      }
    },
    /**
     * 获取利润样式（正数绿色，负数红色）
     */
    getProfitStyle(profit) {
      if (profit == null) {
        return ''
      }
      if (profit < 0) {
        return 'color: #F56C6C; font-weight: bold;'
      } else if (profit > 0) {
        return 'color: #67C23A; font-weight: bold;'
      } else {
        return 'font-weight: bold;'
      }
    }
  }
}
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>

